package com.enjoy.studyfragment.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.enjoy.studyfragment.R;


/**
 * 导航栏 , 包括返回键,标题,右侧按钮.其中:
 * </br>返回键已经实现按键监听
 * </br>右侧按钮已实现按键监听
 */
public class TopBarView extends RelativeLayout
        implements  OnClickListener{

    private RelativeLayout rootView;
    private RelativeLayout rlBar;
    private RelativeLayout rlRight;
    private ImageView backView;
    private TextView rightView;
    private TextView titleView;
    private TextView tvBack;

    private String titleTextStr;
    private int titleTextSize ;
    private int  titleTextColor ;

    private Drawable leftImage ;
    private String rightTextStr ;

    public TopBarView(Context context){
        this(context, null);
    }


    public TopBarView(Context context, AttributeSet attrs) {
        this(context, attrs, R.style.AppTheme);


    }

    public TopBarView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        getConfig(context, attrs);
        initView(context);
    }

    /**
     * 从xml中获取配置信息
     */
    private void getConfig(Context context, AttributeSet attrs) {
        //TypedArray是一个数组容器用于存放属性值
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.Topbar);

        int count = ta.getIndexCount();
        for(int i = 0;i<count;i++)
        {
            int attr = ta.getIndex(i);
            if (attr == R.styleable.Topbar_titleText) {
                titleTextStr = ta.getString(R.styleable.Topbar_titleText);

            } else if (attr == R.styleable.Topbar_titleColor) {// 默认颜色设置为黑色
                titleTextColor = ta.getColor(attr, Color.BLACK);

            } else if (attr == R.styleable.Topbar_titleSize) {// 默认设置为16sp，TypeValue也可以把sp转化为px
                titleTextSize = ta.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
                        TypedValue.COMPLEX_UNIT_SP, 12, getResources().getDisplayMetrics()));

            } else if (attr == R.styleable.Topbar_leftBtn) {
                leftImage = ta.getDrawable(R.styleable.Topbar_leftBtn);

            } else if (attr == R.styleable.Topbar_rightBtn) {
                rightTextStr = ta.getString(R.styleable.Topbar_rightBtn);

            }
        }

        //用完务必回收容器
        ta.recycle();

    }


    private void initView(Context context)
    {
        View layout = LayoutInflater.from(context).inflate(R.layout.item_topbar, this,true);

        rootView = (RelativeLayout) layout.findViewById(R.id.rootView);
        rlBar = (RelativeLayout) layout.findViewById(R.id.rlBar);
        rlRight = (RelativeLayout) layout.findViewById(R.id.rlRight);
        backView = (ImageView) layout.findViewById(R.id.back_image);
        titleView = (TextView) layout.findViewById(R.id.text_title);
        rightView = (TextView) layout.findViewById(R.id.right_image);
        tvBack = (TextView) layout.findViewById(R.id.tvBack);
        backView.setOnClickListener(this);
        rlRight.setOnClickListener(this);
        tvBack.setOnClickListener(this);

        if(null != leftImage)
            backView.setImageDrawable(leftImage);
        if(null != rightTextStr)
            rightView.setText(rightTextStr);
        if(null != titleTextStr)
        {
            titleView.setText(titleTextStr);
            titleView.setTextSize(titleTextSize);
            titleView.setTextColor(titleTextColor);
        }
    }
    /**
     * 获取返回按钮
     * @return
     */
    public ImageView getBackView() {
        return backView;
    }
    public TextView getBackTextView() {
        return tvBack;
    }
    public RelativeLayout getTopBar() {
        return rlBar;
    }

    /**
     * 获取标题控件
     * @return
     */
    public TextView getTitleView() {
        return titleView;
    }

    /**
     * 获取根控件
     * @return
     */
    public RelativeLayout getRootView() {
        return rootView;
    }

    /**
     * 设置标题
     * @param title
     */
    public void setTitle(String title){
        titleView.setText(title);
    }

    /**
     * 获取右侧按钮,默认不显示
     * @return
     */
    public TextView getRightView() {
        return rightView;
    }

    /**
     * 常规标题栏 or 单文字标题栏
     * @param flg
     */
    public void setVisibleBar(boolean flg){
        if(flg){
            rlBar.setVisibility(VISIBLE);
            tvBack.setVisibility(GONE);
        }else {
            rlBar.setVisibility(GONE);
            tvBack.setVisibility(VISIBLE);
        }
    }

    private onTitleBarClickListener onMyClickListener;

    /**
     * 设置按钮点击监听接口
     * @param listener
     */
    public void setClickListener(onTitleBarClickListener listener) {
        this.onMyClickListener = listener;
    }

    /**
     * 导航栏点击监听接口
     */
    public abstract static class onTitleBarClickListener{
        /**
         * 点击返回按钮回调
         */
        protected abstract void onBackClick();

        protected abstract void onRightClick();

        void onBackTextView(){};
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.back_image) {
            if (null != onMyClickListener)
                onMyClickListener.onBackClick();

        } else if (id == R.id.right_image || id == R.id.rlRight) {
            if (null != onMyClickListener)
                onMyClickListener.onRightClick();

        } else if (id == R.id.tvBack) {
            if (null != onMyClickListener)
                onMyClickListener.onBackTextView();

        }
    }


}